﻿using System.Collections;
using System.Collections.Generic;

namespace Miao.ML
{
	public class DictionaryData : GenericReferenceObjectData, IEnumerable<KeyValuePair<WinMLData, WinMLData>>, IEnumerable
	{
		internal readonly Dictionary<WinMLData, WinMLData> dataDictionary = new Dictionary<WinMLData, WinMLData>();

		public override bool isNone => dataDictionary.Count == 0;

		public override bool isGenericType => true;

		public sealed override DataType type => DataType.Dictionary;

		public WinMLData this[WinMLData key] => GetValue(key);

		public void Add(WinMLData key, WinMLData value)
		{
			dataDictionary.Add(key, value);
		}

		public void Remove(WinMLData key)
		{
			dataDictionary.Remove(key);
		}

		private WinMLData GetValue(WinMLData key)
		{
			dataDictionary.TryGetValue(key, out var value);
			return value;
		}

		public override IEnumerable<WinMLData> GetSubData()
		{
			foreach (KeyValuePair<WinMLData, WinMLData> itemData in dataDictionary)
			{
				yield return itemData.Key;
				yield return itemData.Value;
			}
		}

		public override string ToString()
		{
			string text = string.Format("{0}{1}{2}{3}", base.qualifiedName, '&', base.isNull ? "null" : base.referenceID.ToString(), '(');
			if (dataDictionary.Count > 0)
			{
				foreach (KeyValuePair<WinMLData, WinMLData> item in dataDictionary)
				{
					text += $"{item.Key}:{item.Value},";
				}
				text = text.Remove(text.Length - 1, 1);
			}
			return text + ")";
		}

		public IEnumerator<KeyValuePair<WinMLData, WinMLData>> GetEnumerator()
		{
			return dataDictionary.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return dataDictionary.GetEnumerator();
		}
	}
}
